/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegistroUsuario.java
 *
 * Created on 19-jul-2012, 13:53:30
 */
package relojbiometrico;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import reloj.DAO.ConsultasBD;
import reloj.modelo.Personal;
import reloj.modeloDAO.personalDAO;
import reloj.utiles.Mensajes;
import reloj.utiles.Util;

/**
 *
 * @author Dianita
 */
public class RegistroUsuario extends javax.swing.JDialog {

    /** Creates new form RegistroUsuario */
    Mensajes m;
    Personal p;

    public RegistroUsuario(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        m= new Mensajes();
        p= new Personal();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelImage1 = new org.edisoncor.gui.panel.PanelImage();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txttelefono = new javax.swing.JTextField();
        txtCedula = new javax.swing.JTextField();
        txtNombres = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtadireccion = new javax.swing.JTextArea();
        txtApellidos = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        pnlfoto = new org.edisoncor.gui.panel.PanelImage();
        btnBuscarImagen = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        btneliminar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        panelImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/reloj/recursos/fondopantalla.jpg"))); // NOI18N
        panelImage1.setLayout(null);

        jLabel1.setFont(new java.awt.Font("Georgia", 1, 14));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Cédula:");
        panelImage1.add(jLabel1);
        jLabel1.setBounds(20, 70, 130, 20);

        jLabel2.setFont(new java.awt.Font("Georgia", 1, 24));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Registro de Personal");
        panelImage1.add(jLabel2);
        jLabel2.setBounds(160, 20, 320, 30);

        jLabel3.setFont(new java.awt.Font("Georgia", 1, 14));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Nombres:");
        panelImage1.add(jLabel3);
        jLabel3.setBounds(20, 110, 130, 30);

        jLabel4.setFont(new java.awt.Font("Georgia", 1, 14));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel4.setText("Teléfono:");
        panelImage1.add(jLabel4);
        jLabel4.setBounds(20, 200, 130, 20);

        jLabel5.setFont(new java.awt.Font("Georgia", 1, 14));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel5.setText("Apellidos:");
        panelImage1.add(jLabel5);
        jLabel5.setBounds(20, 150, 130, 30);

        jLabel6.setFont(new java.awt.Font("Georgia", 1, 14));
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel6.setText("Dirección:");
        panelImage1.add(jLabel6);
        jLabel6.setBounds(20, 240, 130, 20);
        panelImage1.add(txttelefono);
        txttelefono.setBounds(110, 190, 270, 40);
        panelImage1.add(txtCedula);
        txtCedula.setBounds(110, 70, 230, 30);
        panelImage1.add(txtNombres);
        txtNombres.setBounds(110, 110, 270, 30);

        jtadireccion.setColumns(20);
        jtadireccion.setRows(5);
        jScrollPane1.setViewportView(jtadireccion);

        panelImage1.add(jScrollPane1);
        jScrollPane1.setBounds(110, 240, 270, 60);
        panelImage1.add(txtApellidos);
        txtApellidos.setBounds(110, 150, 270, 30);

        pnlfoto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/reloj/recursos/nino-de-usuario-icono-4518-96.png"))); // NOI18N

        javax.swing.GroupLayout pnlfotoLayout = new javax.swing.GroupLayout(pnlfoto);
        pnlfoto.setLayout(pnlfotoLayout);
        pnlfotoLayout.setHorizontalGroup(
            pnlfotoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 130, Short.MAX_VALUE)
        );
        pnlfotoLayout.setVerticalGroup(
            pnlfotoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 158, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlfoto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlfoto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        panelImage1.add(jPanel1);
        jPanel1.setBounds(400, 70, 150, 180);

        btnBuscarImagen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/reloj/recursos/search_32.png"))); // NOI18N
        btnBuscarImagen.setText("Buscar Imagen");
        btnBuscarImagen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarImagenActionPerformed(evt);
            }
        });
        panelImage1.add(btnBuscarImagen);
        btnBuscarImagen.setBounds(400, 260, 150, 40);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/reloj/recursos/search_32.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        panelImage1.add(jButton1);
        jButton1.setBounds(340, 70, 40, 30);

        btneliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/reloj/recursos/delete_32.png"))); // NOI18N
        btneliminar.setText("Eliminar");
        btneliminar.setEnabled(false);
        btneliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneliminarActionPerformed(evt);
            }
        });
        panelImage1.add(btneliminar);
        btneliminar.setBounds(570, 150, 120, 50);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/reloj/recursos/document_32.png"))); // NOI18N
        jButton3.setText("Nuevo");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        panelImage1.add(jButton3);
        jButton3.setBounds(570, 30, 120, 50);

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/reloj/recursos/save_32.png"))); // NOI18N
        jButton4.setText("Guardar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        panelImage1.add(jButton4);
        jButton4.setBounds(570, 90, 120, 50);

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/reloj/recursos/DeleteRed.png"))); // NOI18N
        jButton5.setText("Salir");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        panelImage1.add(jButton5);
        jButton5.setBounds(570, 270, 120, 50);

        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/reloj/recursos/shield_32.png"))); // NOI18N
        jButton6.setText("Huella");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        panelImage1.add(jButton6);
        jButton6.setBounds(570, 210, 120, 50);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelImage1, javax.swing.GroupLayout.DEFAULT_SIZE, 704, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelImage1, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void limpiarcampos() {
        txtCedula.setText("");
        txtApellidos.setText("");
        txtNombres.setText("");
        txttelefono.setText("");
        jtadireccion.setText("");
    }

    public Personal fijarDatos(Personal p) {
        p.setCedula(Integer.parseInt(txtCedula.getText()));
        p.setApellidos(txtApellidos.getText());
        p.setNombres(txtNombres.getText());
        p.setTelefono(txttelefono.getText());
        p.setDirección(jtadireccion.getText());
        return p;
    }

    public boolean camposLlenos() {
        if (txtCedula.getText().length() != 0 && txtApellidos.getText().length() != 0 && txtNombres.getText().length() != 0 && jtadireccion.getText().length() != 0) {
            return true;
        } else {
            return false;
        }
    }

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:

    limpiarcampos();
    btneliminar.setEnabled(false);
    this.dispose();
    RegistroUsuario ru = new RegistroUsuario(null, true);
    ru.setVisible(true);


}//GEN-LAST:event_jButton3ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:


    Personal personal = new Personal();
    personal = fijarDatos(personal);
    personalDAO pdao = new personalDAO(personal);


    Util u = new Util();
    try {
        if (camposLlenos()) {
            if (u.esCedulaValida(txtCedula.getText())) {

                for (Personal plist : (List<Personal>) new ConsultasBD().PersonalxCedula(txtCedula.getText())) {

                    if (String.valueOf(plist.getCedula()).equals(txtCedula.getText())) {

                        if (m.Pregunta(this, "Desea Actualizar los datos del Personal?") == 0) {
                            Personal personfind = plist;

//                            personfind = pdao.findById(plist.getCedula());

                            personfind.setApellidos(txtApellidos.getText());
                            personfind.setDirección(jtadireccion.getText());
                            personfind.setNombres(txtNombres.getText());
                            personfind.setTelefono(txttelefono.getText());

                            pdao = new personalDAO(personfind);
                            pdao.update();
                            limpiarcampos();
                            this.m.StandarTick(this, "Personal Actualizado con Éxito");
                            return;
                        } else {
                            return;
                        }
                    }
                    System.out.println("finaliza ciclo");
                }

                System.out.println("antes guardado");
                pdao.persist();
                System.out.println("guardado");
                m.StandarTick(this, "Personal Registado con Éxito");
            } else {
                m.StandarWarning(this, "La cédula ingresada no es correcta");
            }
        } else {
            m.StandarWarning(this, "Llene todos los campos");
        }
    } catch (Exception ex) {
        Logger.getLogger(RegistroUsuario.class.getName()).log(Level.SEVERE, null, ex);
    }


}//GEN-LAST:event_jButton4ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    if (txtCedula.getText().length() != 0) {
        if (new Util().esCedulaValida(txtCedula.getText())) {
            List<Personal> plista = new ArrayList<Personal>();
            plista = (List<Personal>) new ConsultasBD().PersonalxCedula(txtCedula.getText());
            for (Personal per : plista) {
                if (per.getCedula() == Integer.parseInt(txtCedula.getText())) {
                    txtApellidos.setText(per.getApellidos());
                    txtCedula.setText(String.valueOf(per.getCedula()));
                    txtNombres.setText(per.getNombres());
                    txttelefono.setText(per.getTelefono());
                    jtadireccion.setText(per.getDirección());
                    btneliminar.setEnabled(true);
                    plista = null;
                } else {
                    m.StandarWarning(this, "No existe ningun personal \n con es número de cédula");
                }
            }
        } else {
            m.StandarWarning(this, "La cédula ingresada no es correcta");
        }
    } else {
        m.StandarWarning(this, "Ingrese un número de cédula");
    }

}//GEN-LAST:event_jButton1ActionPerformed

private void btneliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneliminarActionPerformed
// TODO add your handling code here:
    if (txtApellidos.getText().length() != 0) {
    } else {
        m.StandarWarning(this, "Realize primero una búsqueda");
    }
}//GEN-LAST:event_btneliminarActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
// TODO add your handling code here:
    this.dispose();

}//GEN-LAST:event_jButton5ActionPerformed

private void btnBuscarImagenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarImagenActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_btnBuscarImagenActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
// TODO add your handling code here:
    Marcacion mar = new Marcacion(null, true);
    mar.setLocationRelativeTo(this);
    mar.setVisible(true);
}//GEN-LAST:event_jButton6ActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(RegistroUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(RegistroUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(RegistroUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(RegistroUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the dialog */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                RegistroUsuario dialog = new RegistroUsuario(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscarImagen;
    private javax.swing.JButton btneliminar;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jtadireccion;
    private org.edisoncor.gui.panel.PanelImage panelImage1;
    private org.edisoncor.gui.panel.PanelImage pnlfoto;
    private javax.swing.JTextField txtApellidos;
    private javax.swing.JTextField txtCedula;
    private javax.swing.JTextField txtNombres;
    private javax.swing.JTextField txttelefono;
    // End of variables declaration//GEN-END:variables
}
